package com.watermoniter.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.videogo.constant.IntentConsts;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZGlobalSDK;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZAreaInfo;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.util.LogUtil;
import com.watermoniter.MyApp;
import com.watermoniter.R;
import com.watermoniter.other.EZUtils;
import com.watermoniter.other.SettingIPActivity;
import com.watermoniter.realplay.EZRealPlayActivity;
import com.watermoniter.util.WMPref;
import com.watermoniter.util.WebViewUtil;

import java.util.List;

public class MainActivity extends Activity {

    private GridView mGridView;

    private Context mContext;
    private WebView mWebView;
    private List<EZDeviceInfo> mDeviceslist;
    public final static int REQUEST_CODE = 100;
    public final static int RESULT_CODE = 101;

    private ProductJsManager productJsManager;

    private int currentIndex=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext=this;
        mGridView = (GridView) findViewById(R.id.grid_view);


        mWebView = (WebView) findViewById(R.id.web_view);
        WebViewUtil.initWebView(this,mWebView);
        mWebView.loadUrl(WMPref.getInstance(mContext).getIP()+"/HydrographicWeb/index1.html?dnum="+WMPref.getInstance(mContext).getDeviceNo());
        login();

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if(position<0)return;

                EZDeviceInfo deviceInfo=mDeviceslist.get(position);

                if(deviceInfo.getStatus()==2)return;
                EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo, 0);
                if (cameraInfo == null) {
                    return;
                }
                Intent intent = new Intent(mContext, EZRealPlayActivity.class);
                intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        productJsManager=new ProductJsManager(this);
        mWebView.addJavascriptInterface(productJsManager, "webview");

        setEvent();


    }


    private void setEvent(){
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentIndex==1){
                    return;
                }
                mWebView.loadUrl(WMPref.getInstance(mContext).getIP()+"/HydrographicWeb/index1.html?dnum="+WMPref.getInstance(mContext).getDeviceNo());
                currentIndex=1;
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentIndex==2){
                    return;
                }
                mWebView.loadUrl(WMPref.getInstance(mContext).getIP()+"/HydrographicWeb/index2.html?dnum="+WMPref.getInstance(mContext).getDeviceNo());
                currentIndex=2;
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentIndex==3){
                    return;
                }
                mWebView.loadUrl(WMPref.getInstance(mContext).getIP()+"/HydrographicWeb/index3.html?dnum="+WMPref.getInstance(mContext).getDeviceNo());
                currentIndex=3;
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentIndex==4){
                    return;
                }
                mWebView.loadUrl(WMPref.getInstance(mContext).getIP()+"/HydrographicWeb/index4.html?dnum="+WMPref.getInstance(mContext).getDeviceNo());
                currentIndex=4;
            }
        });
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, SettingIPActivity.class);
                startActivity(intent);
            }
        });
    }



    @Override
    protected void onResume() {
        super.onResume();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mDeviceslist= MyApp.getOpenSDK().getDeviceList(0,20);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            EZCameraListAdapter adapter=new EZCameraListAdapter(mContext,mDeviceslist);
                            mGridView.setAdapter(adapter);
                        }
                    });
                }catch (Exception ex){
                }
            }
        }).start();


    }

    private void login(){
        if (EZGlobalSDK.class.isInstance(MyApp.getOpenSDK())) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        List<EZAreaInfo> areaList = EZGlobalSDK.getInstance().getAreaList();
                        if (areaList != null) {
                            LogUtil.debugLog("application", "list count: " + areaList.size());

                            EZAreaInfo areaInfo = areaList.get(0);
                            EZGlobalSDK.getInstance().openLoginPage(areaInfo.getId());
                        }
                    } catch (BaseException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else {
            EZOpenSDK.getInstance().openLoginPage();
        }
    }

    public class EZCameraListAdapter extends BaseAdapter {
        private static final String TAG = "CameraListAdapter";

        private Context mContext = null;
        private List<EZDeviceInfo> mCameraInfoList = null;


        public List<EZDeviceInfo> getDeviceInfoList() {
            return mCameraInfoList;
        }

        public EZCameraListAdapter(Context context,List<EZDeviceInfo> cameraInfoList) {
            mContext = context;
            mCameraInfoList = cameraInfoList;
        }


        public void addItem(EZDeviceInfo item) {
            mCameraInfoList.add(item);
        }

        public void removeItem(EZDeviceInfo item) {
            for(int i = 0; i < mCameraInfoList.size(); i++) {
                if(item == mCameraInfoList.get(i)) {
                    mCameraInfoList.remove(i);
                }
            }
        }

        public void clearItem() {
            //mExecuteItemMap.clear();
            mCameraInfoList.clear();
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return mCameraInfoList.size();
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public EZDeviceInfo getItem(int position) {
            EZDeviceInfo item = null;
            if (position >= 0 && getCount() > position) {
                item = mCameraInfoList.get(position);
            }
            return item;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 自定义视图
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();

                // 获取list_item布局文件的视图
                convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_gridview, null);

                viewHolder.imgOffline = (ImageView) convertView.findViewById(R.id.img_offline);
                viewHolder.txtName = (TextView) convertView.findViewById(R.id.txt_name);

                // 设置控件集到convertView
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            final EZDeviceInfo deviceInfo = getItem(position);
//            final EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo,0);
            if (deviceInfo != null){
                if (deviceInfo.getStatus() == 2) {
                    viewHolder.imgOffline.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.imgOffline.setVisibility(View.GONE);
                }
                viewHolder.txtName.setText(deviceInfo.getDeviceName());
            }
            return convertView;
        }
    }

    public static class ViewHolder {
        public ImageView imgOffline;
        public TextView txtName;
    }
}
